﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using Zimmerreservierung.Controller;
using Zimmerreservierung.Model;

namespace Tests
{
    [TestClass]
    public class UnitTestKunde
    {
        public TestContext TestContext { get; set; }
        private static KundenController controller;

        [ClassInitialize()]
        public static void ClassInitialize(TestContext context)
        {
            using (SQLiteConnection connection = new SQLiteConnection("Data Source=testdb.dat"))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    command.CommandText = "DROP TABLE IF EXISTS Kunde";
                    command.ExecuteNonQuery();
                    
                    command.CommandText = "CREATE TABLE IF NOT EXISTS Kunde ( ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR(100) NOT NULL, Rechnungsaddresse VARCHAR(100) NOT NULL, "
                                                                            + " Firmenname VARCHAR(100),  Rabatt REAL, Telefonnummer VARCHAR(100) NOT NULL, Faxnummer VARCHAR(100), Email VARCHAR(100), "
                                                                            + " Web VARCHAR(100), Notiz TEXT );";
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }

            controller = KundenController.getInstance();
            controller.startConnection("Data Source=testdb.dat");
        }

        [TestMethod]
        public void testAddKunde()
        {
            try
            {
                bool ok = controller.StoreKunde("TestName", "TestRA 153, 1010 Wien", "testFN", "0.20", "+436991546513854", "+436991546513854", "test@test.at", "http://test.at", "testNotiz");
                Assert.AreEqual(true, ok);

                ok = controller.StoreKunde("TestName1", "TestRA 153, 1010 Wien1", "testFN1", "0.10", "+4369915465138541", "+4369915465138541", "test@test1.at", "http://test1.at", "testNotiz1");
                Assert.AreEqual(true, ok);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testGetKunden()
        {
            try
            {
                List<Kunde> kunden = controller.GetKunden();
                Assert.AreEqual(2, kunden.Count);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testGetKunde()
        {
            try
            {
                Kunde kunde = controller.GetKunde(1);
                Assert.IsNotNull(kunde);

                Assert.AreEqual(1, kunde.Id);
                Assert.AreEqual("TestName", kunde.Name);
                Assert.AreEqual("TestRA 153, 1010 Wien", kunde.Adresse);
                Assert.AreEqual("testFN", kunde.Firmenname);
                Assert.AreEqual("testNotiz", kunde.Notiz);
                Assert.AreEqual(20, kunde.Rabat);
                Assert.AreEqual("+436991546513854", kunde.TelNr);
                Assert.AreEqual("test@test.at", kunde.Mail);
                Assert.AreEqual("http://test.at", kunde.WebAdress);
                Assert.AreEqual("+436991546513854", kunde.FaxNr);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testEditKunde()
        {
            try
            {
                bool ok = controller.UpdateKunde(1, "TestName2", "TestRA 153, 1010 Wien2", "testFN2", "0.40", "+436601546513854", "+436601546513854", "test@test2.at", "http://test2.at", "testNotiz2");
                Assert.AreEqual(true, ok);

                Kunde kunde = controller.GetKunde(1);
                Assert.IsNotNull(kunde);

                Assert.AreEqual(1, kunde.Id);
                Assert.AreEqual("TestName2", kunde.Name);
                Assert.AreEqual("TestRA 153, 1010 Wien2", kunde.Adresse);
                Assert.AreEqual("testFN2", kunde.Firmenname);
                Assert.AreEqual("testNotiz2", kunde.Notiz);
                Assert.AreEqual(40, kunde.Rabat);
                Assert.AreEqual("+436601546513854", kunde.TelNr);
                Assert.AreEqual("test@test2.at", kunde.Mail);
                Assert.AreEqual("http://test2.at", kunde.WebAdress);
                Assert.AreEqual("+436601546513854", kunde.FaxNr);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void testDeleteKunde()
        {
            try
            {
                bool ok = controller.RemoveKunde(1);
                Assert.AreEqual(true, ok);

                Kunde kunde = controller.GetKunde(1);
                Assert.IsNull(kunde);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
    }
}
